<?php
class Navigation_IndexController extends Zend_Controller_Action {
	
	/**
	 * 
	 * Показываем пользователю карту сайта
	 */
	public function indexAction() {
		$query = Doctrine_Query::create()
					 ->from('Navigation_Model_Menuitem')
					 ->orderBy('menu_id ASC')
					 ->where('published = ?', true)
					 ->addOrderBy('lft ASC')
					 ->addOrderBy('ordering ASC');
		$menuItems = array();
		
		$treeObject = Doctrine_Core::getTable('Navigation_Model_Menuitem')->getTree();
		$treeObject->setBaseQuery($query);
		$rootColumnName = $treeObject->getAttribute('rootColumnName');
		
                $lang = $this->_request->getParam('lang');
                
                $baseUrl = $this->view->baseUrl();
                
                $serverUrl = $this->view->serverUrl();
                
                foreach ($treeObject->fetchRoots() as $root) {
			$options = array(
				'root_id' => $root->$rootColumnName
			);
			foreach ($treeObject->fetchTree($options) as $node) {
				$menuItems[$node->id]['name'] = $node->Translation[$this->view->LANG]->label;
				$menuItems[$node->id]['level'] = $node->level;
				$menuItems[$node->id]['id'] = $node->id;
				$node->target ? $menuItems[$node->id]['target'] = '_self' : $menuItems[$node->id]['target'] = '_blank';
				
                                if($node->uri !== '#') {
                                    $path = substr($serverUrl . $node->uri, strlen($baseUrl));
                                    
                                    $menuItems[$node->id]['uri'] = rtrim(substr($baseUrl, strlen($serverUrl)), '/') . '/' . $lang . '/' . ltrim($path, '/');
                                } else {
                                    $menuItems[$node->id]['uri'] = '#';
                                }
			}
		}
		$treeObject->resetBaseQuery();
		$this->view->headTitle($this->view->translate('SITEMAP'));
		
		//устананавливаем метатеги для главной страницы
		$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		if($setting) {
			$this->view->headMeta()->appendName('description', $setting->Translation[$this->view->LANG]->seo_description);
			$this->view->headMeta()->appendName('keywords', $setting->Translation[$this->view->LANG]->seo_keywords);
		}
		$this->view->menuItems = $menuItems;
	}
}